package com.suning.sawp.service.impl.esb;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.esb.CasProductPrice;
import com.suning.sawp.dto.esb.CasProductPriceReq;
import com.suning.sawp.intf.esb.CasGetProductPriceEsbService;
import com.suning.sawp.intf.esb.CasGetProductPriceService;
import com.suning.sawp.service.util.DateUtils;
/**
 * 
 * 解析CAS比价接口服务<br> 
 * 〈功能详细描述〉
 *
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("casGetProductPriceService")
public class CasGetProductPriceServiceImpl implements CasGetProductPriceService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CasGetProductPriceServiceImpl.class);
	
	private CasGetProductPriceEsbService casGetProductPriceEsbService;
	
    @Override
    public CasProductPrice getProductPrice(String dateStr, List<String> productIdList, boolean historyFlag) {
        Map<String, Object> params = new HashMap<String, Object>();
        CasProductPriceReq req = new CasProductPriceReq();
        
        // dateStr为空则表示默认设为今日
        String newDateStr = dateStr; 
        if (StringUtils.isBlank(newDateStr)) {
            newDateStr = DateUtils.getDateStrByFormat(new Date(), DateUtils.YYYYMMDD_PATTERN);
            req.setDateStr(newDateStr);
        }
        params.put("dateStr", newDateStr);
        if (CollectionUtils.isNotEmpty(productIdList)) {
            StringBuilder builder = new StringBuilder();
            for (String productId : productIdList) {
                builder.append(productId).append(",");
            }
            builder.deleteCharAt(builder.length() - 1);
            params.put("productIds", builder.toString());
            req.setProductIds(builder.toString());
        }
        params.put("historyFlag", historyFlag);
        if (historyFlag){
        	req.setHistoryPriceFlag("1");
        }
        
        CasProductPrice casProductPrice = null;
        //调用ESB接口
        MbfResponse resp = casGetProductPriceEsbService.getProductPrice(req);
        //正常处理后, 进行数据类型转换
        if (null != resp && resp.isSucceed()) {
    		try {
                casProductPrice = (CasProductPrice) resp.getOutput().getMbfBody(CasProductPrice.class);
                casProductPrice.setSuccess(true);
    		} catch(Exception e) {
    			LOGGER.error("转换对象失败, ", e);
    			casProductPrice = defaultBean();
    		}
        } else {
        	casProductPrice = defaultBean();
        }
        
        return casProductPrice;
    }

	public CasGetProductPriceEsbService getCasGetProductPriceEsbService() {
		return casGetProductPriceEsbService;
	}

	@EsbEIHttpWired
	public void setCasGetProductPriceEsbService(CasGetProductPriceEsbService casGetProductPriceEsbService) {
		this.casGetProductPriceEsbService = casGetProductPriceEsbService;
	}
	 
	/**
	 * 异常状态,默认对象
	 * @return
	 */
	private CasProductPrice defaultBean() {
		CasProductPrice casProductPrice = new CasProductPrice();
    	casProductPrice.setSuccess(false);
		casProductPrice.setCode(ErrorCodeConstants.ERR_0002);
		casProductPrice.setDesc(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
		return casProductPrice;
	}
}
